package com.ccctop.crowd.handler;

import com.ccctop.crowd.api.MySQLRemoteService;
import com.ccctop.crowd.constant.CrowdConstant;
import com.ccctop.crowd.entity.po.TFundRequest;
import com.ccctop.crowd.entity.po.TFundRequestInfo;
import com.ccctop.crowd.entity.po.TInvestor;
import com.ccctop.crowd.entity.vo.*;
import com.ccctop.crowd.service.FundService;
import com.ccctop.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/fund")
public class FundHandler {
    @Autowired
    MySQLRemoteService mySQLRemoteService;
    @Autowired
    FundService fundService;


    @RequestMapping("/info/{userId}")
    @ResponseBody
    ResultEntity<List<FundRequestVo>> fundInfo(@PathVariable("userId") Integer userId){
        try {
            return fundService.fundInfo(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/agree")
    @ResponseBody
    ResultEntity<String> fundAgree(@RequestBody FundVoteVo fundVoteVo){
        try {
            return fundService.fundAgree(fundVoteVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/refuse")
    @ResponseBody
    ResultEntity<String> fundRefuse(@RequestBody FundVoteVo fundVoteVo){
        try {
            return fundService.fundRefuse(fundVoteVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/page/{projectId}")
    String fundPage(@PathVariable("projectId") Integer projectId, Model model){
        model.addAttribute("projectId",projectId);
        return "fundApplicationPage";
    }

    @RequestMapping(value = "/request",method = RequestMethod.POST)
    @ResponseBody
    ResultEntity<String> fundRequest(@RequestBody TFundRequestVo fundRequest, HttpSession session){
        MemberLoginVO memberLoginVO = (MemberLoginVO) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        fundRequest.setFundApplicantId(memberLoginVO.getId());
        fundRequest.setStatus(0);
        try {
            return fundService.createFundRequest(fundRequest);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/support")
    @ResponseBody
    ResultEntity<String> supportProject(@RequestBody InvestorVo investor){
        try{
            fundService.supportProject(investor);
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/withdraw")
    @ResponseBody
    ResultEntity<String> tryWithdraw(@RequestBody WithdrawVo withdrawVo){
        try{
            fundService.tryWithdraw(withdrawVo);
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }


}
